package com.arithmetic;

import java.util.ArrayList;
import java.util.List;

public class A {
	static class Node {
		public int value;
		public List<Node> children = new ArrayList<>();
		public Node(int d){
			value = d;
		}
	}
	
	public static void process(Node node) { 
		for (int i = 0; i < node.children.size(); i++) {
			Node child = node.children.get(i);
			if (child.value < node.value) {
				process(child);
			}
		}
		System.out.println(node.value);
		for (int i = 0; i < node.children.size(); i++) {
			Node child = node.children.get(i);
			if (child.value >= node.value) {
				process(child);
			}
		}
	}
	
	public static void main(String[] args) {
		Node root = new Node(5);
		Node r0 = new Node(3);
		Node r1 = new Node(6);
		Node r01 = new Node(8);
		Node r02 = new Node(9);
		Node r11 = new Node(4);
		Node r12 = new Node(7);
		List ch1 = new ArrayList<>();
		ch1.add(r0);
		ch1.add(r1);
		root.children = ch1;
		
		List ch2 = new ArrayList<>();
		ch2.add(r01);
		ch2.add(r02);
		r0.children = ch2;
		
		List ch3 = new ArrayList<>();
		ch2.add(r11);
		ch2.add(r12);
		r1.children = ch3;
		
		process(root);
		
		
	}
}
